package edu.buct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import core.api.BaseApiService;

import core.api.BaseResponse;
import edu.buct.entity.Total;
import edu.buct.mapper.TotalMapper;
import edu.buct.service.TotalService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class TotalServiceImpl extends BaseApiService implements TotalService {

    @Resource
    private TotalMapper totalMapper;

    @Override
    public BaseResponse<Total> selectAll() {
        // 开始查询
        QueryWrapper<Total> queryWrapper = new QueryWrapper<>();
        // column ->写在数据库字段，参数1的值==参数2的值

        List<Total> c1List = totalMapper.selectList(queryWrapper);
        return setResultSuccessData(c1List);
    }

    @Override
    public BaseResponse insert(Total total) {
        QueryWrapper<Total> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", total.getId());
        Total one = totalMapper.selectOne(queryWrapper);
        if (one != null) {
            return setResultError("already exist");
        }
        return totalMapper.insert(total) > 0 ? setResultSuccess() : setResultError("insert fail");
    }

    @Override
    public BaseResponse delete(Integer id) {
        QueryWrapper<Total> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Total one = totalMapper.selectOne(queryWrapper);
        if (one == null) {
            return setResultError("not exist");
        }
        return totalMapper.deleteById(id) > 0 ? setResultSuccess() : setResultError("delete fail");
    }

    @Override
    public BaseResponse<Total> updateInfo(Total total) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", total.getId());
        return totalMapper.update(total, updateWrapper) > 0 ? setResultSuccess() : setResultError("update fail");
    }
}
